﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SunXNA.RainbowXNA.Skybox
{
    class CSkybox
    {
        private Model m_model;

        public CSkybox(ContentManager contentManager, string AssetName)
        {
            m_model = contentManager.Load<Model>(AssetName);

            foreach (ModelMesh mesh in m_model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    //effect.EnableDefaultLighting();
                    effect.DiffuseColor = Color.White.ToVector3();
                    effect.SpecularColor = Color.Black.ToVector3();
                }
            }
        }

        public void Draw( GraphicsDevice device, ref Matrix m4View, ref Matrix m4Projection )
        {
            device.RenderState.DepthBufferEnable = false;

            Matrix m4SkyBoxView = m4View;
            m4SkyBoxView.Translation = Vector3.Zero;

            Matrix[] limbPositions = new Matrix[m_model.Bones.Count];
            m_model.CopyAbsoluteBoneTransformsTo(limbPositions);

            foreach (ModelMesh mesh in m_model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Projection = m4Projection;
                    effect.View = m4SkyBoxView;
                    effect.World = limbPositions[mesh.ParentBone.Index] * Matrix.Identity;
                }

                mesh.Draw();
            }

            device.RenderState.DepthBufferEnable = true;
        }
    }
}
